#include <glib.h>		// Glib函数库


static GMutex* mutex = NULL;
static gboolean t1_end = FALSE;
static gboolean t2_end = FALSE;

typedef struct _Arg
{
	GMainLoop* loop;
	gint max;
}Arg;

void t_1 (Arg* arg) {
	int i;
	for (i = 0; i < arg->max; ++i) {
		if (g_mutex_trylock(mutex) == FALSE) {
			g_print("%d:线程2锁定了互斥对象\n", i);
			g_mutex_unlock(mutex);
		} else {
			g_usleep(10);
		}
	}
	t1_end = TRUE;
}

void t_2 (Arg* arg) {
	int i;
	for (i = 0; i < arg->max; ++i) {
		if (g_mutex_trylock(mutex) == FALSE) {
			g_print("%d:线程1锁定了互斥对象\n", i);
			g_mutex_unlock(mutex);
		} else {
			g_usleep(10);
		}
	}
	t2_end = TRUE;
}

void t_3 (Arg* arg) {
	for (;;) {
		if (t1_end && t2_end) {
			g_main_loop_quit(arg->loop);
			break;
		}
	}
}

int main() {
	GMainLoop* mLoop;
	Arg* arg;
	if (!g_thread_supported()) {
		g_thread_init(NULL);
	}

	mLoop = g_main_loop_new(NULL, FALSE);
	arg = g_new(Arg, 1);
	arg->loop = mLoop;
	arg->max = 11;

	mutex = g_mutex_new();
	g_thread_create(t_1, arg, TRUE, NULL);
	g_thread_create(t_2, arg, TRUE, NULL);
	g_thread_create(t_3, arg, TRUE, NULL);

	g_main_loop_run(mLoop);
	g_print("线程3退出事件循环\n");
	g_mutex_free(mutex);
	g_print("释放互斥对象\n");
	g_free(arg);
	g_print("释放参数所用内存\n");

	return 0;
}


